#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - 预授权撤销功能使用示例
"""

import os
import sys

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.services.trans.common.pre_auth_revoke_service import PreAuthRevokeService
from jlpay.model.trans.common.common_trans import PreAuthRevokeRequest


class PreAuthRevokeExample(BaseExample):
    """预授权撤销示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.trans.common.pre_auth_revoke")
        self.service = PreAuthRevokeService(self.config)
    
    def build_pre_auth_revoke_request(self):
        """
        构建预授权撤销请求
        
        Returns:
            PreAuthRevokeRequest实例
        """
        request = PreAuthRevokeRequest()
        request.set_mch_id("849584358120018")
        request.set_term_no("60131234")
        request.set_pay_type("wxpay")
        request.set_out_trade_no("ld201911023465529")
        request.set_ori_out_trade_no("ld2019102216465525")
        request.set_ori_transaction_id("80011557201909171853011568")
        request.set_guarantee_auth_code("134965710109584780")
        request.set_total_fee("100")
        request.set_remark("测试预授权撤销")
        request.set_device_info("N50007A8890")
        request.set_mch_create_ip("127.0.0.1")
        request.set_trans_address("北京市朝阳区")
        request.set_area_code("110000")
        request.set_longitude("116.397128")
        request.set_latitude("39.916527")
        return request
    
    def demo(self):
        """执行预授权撤销示例"""
        self.logger.info("开始执行预授权撤销示例")
        
        # 构建请求
        request = self.build_pre_auth_revoke_request()
        
        # 发送请求并处理响应
        self.send_request_and_handle_response(
            self.service.pre_auth_revoke,
            "预授权撤销",
            request
        )
        
        self.logger.info("预授权撤销示例执行完毕")


if __name__ == "__main__":
    logger = PreAuthRevokeExample().logger
    logger.info("==================================================")
    logger.info("===== 预授权撤销示例 开始 =====")
    logger.info("==================================================")
    
    example = PreAuthRevokeExample()
    example.demo()
    
    logger.info("==================================================")
    logger.info("===== 预授权撤销示例执行完毕 =====")
    logger.info("==================================================") 